// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-01

package profile

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/responses"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib/authorizer"
)

// Logic
// 加载用户资料.
type Logic struct {
	auth     *authorizer.Authentication
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		response: NewResponse(),
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	o.auth = authorizer.New().Get(i)
	return
}

// Run
// 处理过程.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)

	// 1. 加载数据.
	for _, fn := range []func(ctx context.Context) error{
		o.RunProfile,
		o.RunStatistic,
		o.RunArcades,
	} {
		if err = fn(ctx); err != nil {
			break
		}
	}

	// 2. 响应结果.
	return res.With(o.response)
}

// RunArcade
// 钓场资料.
func (o *Logic) RunArcade(ctx context.Context, admin *models.ArcadeAdmins) (err error) {
	var (
		has      bool
		model    *models.Arcades
		setting  *models.ArcadeSetting
		settings = services.NewArcadeSettingService()
	)

	// 1. 钓场信息.
	if model, has, err = services.NewArcadesService().Dao.GetById(ctx, admin.ArcadeId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 忽略钓场.
	if !has || !model.State.IsEnabled() {
		return
	}

	// 3. 读取设置.
	if setting, has, err = settings.Dao.GetByMap(ctx, map[string]any{
		"arcade_id": admin.ArcadeId,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 4. 处理结果.
	if has {
		o.response.Arcades = append(o.response.Arcades,
			(&responses.ArcadeResponse{}).With(model).WithSetting(setting),
		)
	}
	return
}

// RunArcades
// 钓场列表.
func (o *Logic) RunArcades(ctx context.Context) (err error) {
	var list []*models.ArcadeAdmins

	// 1. 读取钓场.
	//    用户管理的钓场列表.
	if list, err = services.NewArcadeAdminsService().Dao.ListByMap(ctx, map[string]any{
		"user_id": o.auth.UserId,
		"state":   kind.ArcadeAdminStateEnabled,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 遍历钓场.
	for _, item := range list {
		if err = o.RunArcade(ctx, item); err != nil {
			break
		}
	}
	return
}

// RunProfile
// 用户资料.
func (o *Logic) RunProfile(ctx context.Context) (err error) {
	var (
		has   bool
		model *models.Users
	)

	// 1. 读取资料.
	if model, has, err = services.NewUsersService().Dao.GetById(ctx, o.auth.UserId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 用户异常.
	if !has || !model.State.IsNormal() {
		err = errs.ErrUserNotFound
		return
	}

	// 3. 绑定数据.
	o.response.Profile.With(model)
	return
}

// RunStatistic
// 账号统计.
func (o *Logic) RunStatistic(ctx context.Context) (err error) {
	var (
		has   bool
		model *models.UserStatistic
	)

	// 1. 读取统计.
	if model, has, err = services.NewUserStatisticService().Dao.GetBy(ctx, "user_id", o.auth.UserId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrUserNotFound
		return
	}

	// 2. 绑定数据.
	o.response.Statistic.With(model)
	return
}
